package com.lws.hellojersey.resources;

import com.lws.hellojersey.bean.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

/**
 * Created by lvws on 2016/4/28.
 */
@Component
@Provider
@Scope("prototype")
@Path("/demoresource")
public class DemoResource {

    @GET
    @Path("/getUser")
    @Produces(MediaType.APPLICATION_JSON)
    public User getUser() {
        User user = new User();
        user.setUserName("sed");
        user.setAge(29);
        return user;
    }

    @GET
    @Path("getUser/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getUserId(@PathParam("id") String id) {
        return id;
    }

    @GET
    @Path("getId")
    @Produces(MediaType.TEXT_PLAIN)
    public String getId(@QueryParam("id") String id) {
        return id;
    }

    @POST
    @Path("insertUser")
    @Produces(MediaType.APPLICATION_JSON)  //指定发送请求的 MIME 类型
    @Consumes(MediaType.APPLICATION_XML)  //指定响应所能接受的 MIME 类型
    public User insertuser(User user) {
        return user;
    }

    @POST
    @Path("insertUserFromBean")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public User inserthome(@BeanParam User user) {
        return user;
    }

    @PUT
    @Path("insertUserFromForm")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_PLAIN)
    public String insertuser(@FormParam("username") String userName, @FormParam("age") int age) {
        User user = new User();
        user.setUserName(userName);
        user.setAge(age);
        return user.toString();
    }

    @POST
    @Path("map")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMap(MultivaluedMap<String, String> formParams) {
        return formParams.toString();
    }

    @DELETE
    @Path("deleteUser/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteUser(@PathParam("id") String id) {
        return "delete " + id;
    }

}
